package com.match.competition_apply.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.match.commonutils.Result;
import com.match.competition_apply.entity.CompetitionLever;
import com.match.competition_apply.service.CompetitionLeverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AdiosF
 * @since 2021-09-23
 */
@Api(tags = "竞赛等级模块")

@RestController
@RequestMapping("/competition_apply/competition-lever")
public class CompetitionLeverController {
      @Autowired
      private CompetitionLeverService competitionLeverService;

      @ApiOperation("添加竞赛等级")
      @PostMapping("/addCompetitionLever")
      public Result addCompetitionLever(@ApiParam(required = true)
                                   @RequestBody CompetitionLever competitionLever){
          QueryWrapper<CompetitionLever> competitionLeverQueryWrapper=new QueryWrapper<>();
          String title = competitionLever.getTitle();
          if (!StringUtils.isEmpty(title)){
              competitionLeverQueryWrapper.eq("title",title);
          }
          int count = competitionLeverService.count(competitionLeverQueryWrapper);
          if (count>0){
              return Result.error().message("请勿重复添加竞赛等级");
          }
          boolean save = competitionLeverService.save(competitionLever);
          if (!save){
              return Result.error().message("竞赛等级添加失败");
          }
          return Result.ok().message("竞赛等级添加成功");
      }


      @ApiOperation("获得所有的竞赛等级")
      @GetMapping("/getAllCompetitionLever")
      public Result getAllCompetitionLever(){
          QueryWrapper<CompetitionLever> competitionLeverQueryWrapper=new QueryWrapper<>();
          competitionLeverQueryWrapper.orderByAsc("gmt_create");
          List<CompetitionLever> list = competitionLeverService.list(competitionLeverQueryWrapper);
          if (list.isEmpty()){
              return Result.error().message("竞赛等级库为空");
          }
          return Result.ok().data("CompetitionList",list);
      }
}

